/**
 * 根据给定字符串生成一个唯一不变的渐变色
 *
 * @param str - 用于生成渐变的种子字符串，通常是用户昵称等
 * @returns 渐变 CSS background 字符串
 */
export function generateGradientBackgroundByString(str: string) {
  const hash = str.split('').reduce((acc, char) => {
    return char.charCodeAt(0) + ((acc << 5) - acc)
  }, 0)
  const gradient = gradients[Math.abs(hash) % gradients.length]
  return `linear-gradient(45deg, ${gradient[0]}, ${gradient[1]})`
}

export function generateRandomColorByString(str: string) {
  const hash = str.split('').reduce((acc, char) => {
    return char.charCodeAt(0) + ((acc << 5) - acc)
  }, 0)
  return colors[Math.abs(hash) % gradients.length] || colors[7]
}

export const gradients = [
  ['#0061ff', '#60efff'],
  ['#696eff', '#f8acff'],
  ['#595cff', '#c6f8ff'],
  ['#84ffc9', '#aab2ff'],
  ['#ffa585', '#ffeda0'],
  ['#f9c58d', '#f492f0'],
  ['#ff5858', '#ffc8c8'],
  ['#f7d486', '#f27a7d'],
  ['#c9def4', '#f5ccd4'],
  ['#f5ccd4', '#b8a4c9'],
  ['#b597f6', '#96c6ea'],
  ['#f6cfbe', '#b9dcf2'],
  ['#f5bfd7', '#abc9e9'],
  ['#8de9d5', '#32c4c0'],
  ['#ffb88e', '#ea5753'],
  ['#f5e6ad', '#f13c77'],
  ['#82f4b1', '#30c67c'],
  ['#d4acfb', '#b84fce'],
  ['#ffcb6b', '#3d8bff'],
  ['#7ef29d', '#0f68a9'],
  ['#919bff', '#133a94'],
  ['#ffa8bd', '#242acf'],
  ['#9d80cb', '#f7c2e6'],
  ['#6d90b9', '#bbc7dc'],
  ['#9bb2e5', '#698cbf'],
  ['#2278fb', '#6bdfdb'],
  ['#adfda2', '#11d3f3'],
  ['#727a9a', '#d8dbe9'],
]

const colors = [
  '#ef4444',
  '#f97316',
  '#f59e0b',
  '#eab308',
  '#84cc16',
  '#22c55e',
  '#10b981',
  '#14b8a6',
  '#06b6d4',
  '#0ea5e9',
  '#3b82f6',
  '#6366f1',
  '#8b5cf6',
  '#a855f7',
  '#d946ef',
  '#ec4899',
  '#f43f5e',
]
